﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using XiAnAirport.DAL;
using XiAnAirport.Models;

namespace XiAnAirport.BUS
{
    public class UserRoleBUS
    {
        public ResponseMessage AddUser(UserInfo userinfo)
        {
            UserRoleDAL da = new UserRoleDAL();
            string result = string.Empty;
            if (da.CheckUserName(userinfo.username, -1))
            {
                result = "账户已存在";
            }
            else
            {
                result = da.InsertUser(userinfo);
            }
            ResponseMessage message = new ResponseMessage();
            if (result.Contains("success"))
            {
                message.errcode = 0;
                message.errmsg = "";
                message.data = null;
                message.total = 1;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = result;
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        public ResponseMessage EditUser(UserInfo userinfo)
        {
            UserRoleDAL da = new UserRoleDAL();
            string result = string.Empty;
            if (da.CheckUserName(userinfo.username, userinfo.id))
            {
                result = "用户名已存在";
            }
            else if (da.CheckManage(userinfo))
            {
                result = "维保主管已存在";
            }
            else
            {
                userinfo.isInspection = Convert.ToInt32(da.LoadRole(userinfo.roleid).isInspection);
                result = da.EditUser(userinfo);
            }
            ResponseMessage message = new ResponseMessage();
            if (result.Contains("success"))
            {
                message.errcode = 0;
                message.errmsg = "";
                message.data = null;
                message.total = 1;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = result;
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        public ResponseMessage DelUser(UserInfo userinfo)
        {
            UserRoleDAL da = new UserRoleDAL();
            string result = string.Empty;

            result = da.DelUser(userinfo.id);

            ResponseMessage message = new ResponseMessage();
            if (result.Contains("success"))
            {
                message.errcode = 0;
                message.errmsg = "";
                message.data = null;
                message.total = 1;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = result;
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        public ResponseMessage QueryUser(UserInfo userinfo, string loginname)
        {
            ResponseMessage message = new ResponseMessage();

            UserRoleDAL da = new UserRoleDAL();
            List<UserInfo> content = da.QueryUser(userinfo, loginname);

            message.errcode = 0;
            message.errmsg = "";
            message.data = content;
            message.total = da.QueryUserCount(userinfo, loginname);

            return message;
        }

        public ResponseMessage LoadUser(int userid)
        {
            ResponseMessage message = new ResponseMessage();
            UserRoleDAL da = new UserRoleDAL();
            UserInfo content = da.LoadUser(userid);
            if (content != null)
            {
                UserInfo user = da.CheckExistSelina();
                if (user != null)
                {
                    content.IsSelina = 1;
                    content.SelinUserid = user.username;
                }
                else
                {
                    content.IsSelina = 0;
                    content.SelinUserid = "";
                }
                if (!string.IsNullOrEmpty(content.creationDate))
                {
                    content.creationDate = Convert.ToDateTime(content.creationDate).ToString("yyyy-MM-dd HH:mm:ss");
                }
                if (!string.IsNullOrEmpty(content.loginTime))
                {
                    content.loginTime = Convert.ToDateTime(content.loginTime).ToString("yyyy-MM-dd HH:mm:ss");
                }

                message.errcode = 0;
                message.errmsg = "";
                message.data = content;
                message.total = 1;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "未查询到相关信息";
                message.data = null;
                message.total = 0;
            }

            return message;
        }

        public ResponseMessage ResetPwd(UserInfo userinfo)
        {
            UserRoleDAL da = new UserRoleDAL();
            string result = da.ResetPwd(userinfo.id, userinfo.password);

            ResponseMessage message = new ResponseMessage();
            if (result.Contains("success"))
            {
                message.errcode = 0;
                message.errmsg = "";
                message.data = null;
                message.total = 1;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = result;
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        public ResponseMessage AddRole(Role roleinfo)
        {
            UserRoleDAL da = new UserRoleDAL();
            string result = string.Empty;
            if (da.CheckRoleName(roleinfo.typename, -1))
            {
                result = "该角色已存在";
            }
            else
            {
                result = da.AddRole(roleinfo);
            }

            ResponseMessage message = new ResponseMessage();
            if (result.Contains("success"))
            {
                message.errcode = 0;
                message.errmsg = "";
                message.data = null;
                message.total = 1;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = result;
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        public ResponseMessage EditRole(Role roleinfo)
        {
            UserRoleDAL da = new UserRoleDAL();
            string result = string.Empty;
            if (da.CheckRoleName(roleinfo.typename, roleinfo.id))
            {
                result = "角色名已存在";
            }
            else
            {
                result = da.EditRole(roleinfo);
            }

            ResponseMessage message = new ResponseMessage();
            if (result.Contains("success"))
            {
                message.errcode = 0;
                message.errmsg = "";
                message.data = null;
                message.total = 1;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = result;
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        public ResponseMessage DelRole(int roleid)
        {
            UserRoleDAL da = new UserRoleDAL();
            string result = string.Empty;
            if (da.CheckRoleUser(roleid))
            {
                result = "该权限下有对应用户，请先删除";
            }
            else
            {
                result = da.DelRole(roleid);
            }
            ResponseMessage message = new ResponseMessage();
            if (result.Contains("success"))
            {
                message.errcode = 0;
                message.errmsg = "";
                message.data = null;
                message.total = 1;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = result;
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        public ResponseMessage BatchDelRole(string ids)
        {
            UserRoleDAL da = new UserRoleDAL();
            string result = string.Empty;

            result = da.BatchDelRole(ids);
            ResponseMessage message = new ResponseMessage();
            if (result.Contains("success"))
            {
                message.errcode = 0;
                message.errmsg = "";
                message.data = null;
                message.total = 1;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = result;
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        public ResponseMessage LoadRole(int roleid)
        {
            UserRoleDAL da = new UserRoleDAL();
            ResponseMessage message = new ResponseMessage();
            Role content = da.LoadRole(roleid);
            List<Menu> list = da.QueryParentMenu();
            if (content != null)
            {
                string menurole = "," + content.menuRole + ",";
                foreach (var item in list)
                {
                    string itemrole = "," + item.id + ",";
                    if (menurole.Contains(itemrole))
                    {
                        menurole = menurole.Replace(itemrole, ",");
                    }
                }
                content.menuRole = menurole.TrimEnd(',').TrimStart(',');
                message.errcode = 0;
                message.errmsg = "";
                message.data = content;
                message.total = 1;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "未查询到相关信息";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        public ResponseMessage QueryRole(Role roleinfo)
        {
            ResponseMessage message = new ResponseMessage();

            UserRoleDAL da = new UserRoleDAL();
            List<Role> content = da.QueryRole(roleinfo);

            message.errcode = 0;
            message.errmsg = "";
            message.data = content;
            message.total = da.QueryRoleCount(roleinfo);

            return message;
        }

        public ResponseMessage AssignPermissions(UserInfo userinfo)
        {
            UserRoleDAL da = new UserRoleDAL();
            ResponseMessage message = new ResponseMessage();

            string result = da.AssignPermissions(userinfo);

            if (result.Contains("success"))
            {
                message.errcode = 0;
                message.errmsg = "";
                message.data = null;
                message.total = 1;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = result;
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        public ResponseMessage UpdateInspectionAuthority(Role role)
        {
            UserRoleDAL da = new UserRoleDAL();
            ResponseMessage message = new ResponseMessage();

            string result = da.UpdateInspectionAuthority(role);
            if (result == "success")
            {
                message.errcode = 0;
                message.errmsg = "";
                message.data = "";
                message.total = 1;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = result;
                message.data = "";
                message.total = 0;
            }

            return message;
        }

        public ResponseMessage BatchDelUser(string ids)
        {
            UserRoleDAL da = new UserRoleDAL();
            string result = string.Empty;

            result = da.BatchDelUser(ids);

            ResponseMessage message = new ResponseMessage();
            if (result.Contains("success"))
            {
                message.errcode = 0;
                message.errmsg = "";
                message.data = null;
                message.total = 1;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = result;
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        public ResponseMessage Unlock(int id)
        {
            ResponseMessage message = new ResponseMessage();

            UserRoleDAL da = new UserRoleDAL();
            bool result = da.UnlockLoginFail(id);
            if (result)
            {
                message.errcode = 0;
                message.errmsg = "";
                message.data = null;
                message.total = 1;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "解锁失败";
                message.data = null;
                message.total = 0;
            }
            return message;
        }
    }
}
